package com.hanter.android.radui.mvp;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.StyleRes;

import com.hanter.android.radui.R;
import com.hanter.android.radwidget.statusbar.StatusBarHelper;

public class TitleBarHelper {

    public static int titleBarHeight;

    private Context context;
    public View rootView;
    public TextView title;
    public ViewGroup titleBar; // 包含假状态栏的标题栏ViewGroup
    public ViewGroup realTitleBar; // 标题栏ViewGroup（不包含假状态栏）
    public View titleBarLine;
    public View compatPrimaryDark;
    public TextView leftBarButton;
    public LinearLayout leftBar;
    public TextView rightBarButton;
    public LinearLayout rightBar;
    public ViewGroup baseContentContainer;

    public static void initTitleBarHeight(View view) {
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        layoutParams.height = titleBarHeight;
        view.setLayoutParams(layoutParams);
    }

    public static void initPrimaryDarkView(View view) {
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        layoutParams.height = StatusBarHelper.getStatusBarHeight(view.getContext());
        view.setLayoutParams(layoutParams);
    }

    public TitleBarHelper(@NonNull View view) {
        this(view, 0);
    }

    public TitleBarHelper(@NonNull View view, @StyleRes int styleRes) {
        context = view.getContext();

        compatPrimaryDark = view.findViewById(R.id.compatPrimaryDark);
        titleBar = view.findViewById(R.id.compatTitleBar);
        realTitleBar = view.findViewById(R.id.titleBar);
        title = view.findViewById(R.id.title);
        leftBar = view.findViewById(R.id.leftBar);
        leftBarButton = view.findViewById(R.id.leftBarButton);
        rightBar = view.findViewById(R.id.rightBar);
        rightBarButton = view.findViewById(R.id.rightBarButton);
        titleBarLine = view.findViewById(R.id.titleBarLine);

        baseContentContainer = getContentContainer(view);

        initAttrs(0);
    }

    public TitleBarHelper(@NonNull Activity activity) {
        this(activity, 0);
    }

    public TitleBarHelper(@NonNull Activity activity, @StyleRes int styleRes) {
        context = activity;

        compatPrimaryDark = activity.findViewById(R.id.compatPrimaryDark);
        title = activity.findViewById(R.id.title);
        titleBar = activity.findViewById(R.id.compatTitleBar);
        realTitleBar = activity.findViewById(R.id.titleBar);
        titleBarLine = activity.findViewById(R.id.titleBarLine);
        leftBarButton = activity.findViewById(R.id.leftBarButton);
        leftBar = activity.findViewById(R.id.leftBar);
        rightBarButton = activity.findViewById(R.id.rightBarButton);
        rightBar = activity.findViewById(R.id.rightBar);

        initAttrs(styleRes);

        baseContentContainer = activity.findViewById(R.id.base_content_container);
    }

    private void initAttrs(@StyleRes int styleRes) {
        TypedArray t;
        if (styleRes != 0) {
            t = context.obtainStyledAttributes(styleRes, R.styleable.TitleBar);
        } else {
            t = context.obtainStyledAttributes(null, R.styleable.TitleBar, R.attr.titleBarStyle,
                    R.style.TitleBar);
        }

        String titleText = t.getString(R.styleable.TitleBar_title);
        String leftButtonText = t.getString(R.styleable.TitleBar_leftBarButtonText);
        String rightButtonText = t.getString(R.styleable.TitleBar_rightBarButtonText);
        int lineColor = t.getColor(R.styleable.TitleBar_titleBarLineColor,
                context.getResources().getColor(R.color.default_title_bar_line_color));
        int visibility = t.getBoolean(R.styleable.TitleBar_titleBarLineVisible, false) ? View.VISIBLE : View.GONE;
        int barButtonTextColor = t.getColor(R.styleable.TitleBar_titleBarButtonTextColor, 0xFF3B3D47);

        int colorPrimary = t.getColor(R.styleable.TitleBar_colorPrimary,
                context.getResources().getColor(R.color.colorPrimary));

        int colorPrimaryDark = t.getColor(R.styleable.TitleBar_colorPrimaryDark,
                context.getResources().getColor(R.color.colorPrimaryDark));

        title.setText(titleText);
        title.setTextColor(t.getColor(R.styleable.TitleBar_titleBarTextColor, 0xFF3B3D47));
        leftBarButton.setText(leftButtonText);
        leftBarButton.setTextColor(barButtonTextColor);
        rightBarButton.setText(rightButtonText);
        rightBarButton.setTextColor(barButtonTextColor);
        titleBarLine.setBackgroundColor(lineColor);
        titleBarLine.setVisibility(visibility);

        titleBar.setBackgroundColor(colorPrimary);
        compatPrimaryDark.setBackgroundColor(colorPrimaryDark);

        t.recycle();
    }

    protected ViewGroup getContentContainer(@NonNull View view) {
        return view.findViewById(R.id.base_content_container);
    }

    public void initTitleBar() {
        initStatusBarHeight();

        ViewGroup.LayoutParams params = titleBar.getLayoutParams();
        params.height = titleBarHeight;
        titleBar.setLayoutParams(params);

        ViewGroup.LayoutParams darkParams = compatPrimaryDark.getLayoutParams();
        darkParams.height = StatusBarHelper.getCompatStatusBarHeight(context);
        compatPrimaryDark.setLayoutParams(darkParams);

        FrameLayout.LayoutParams contentParams = (FrameLayout.LayoutParams)
                baseContentContainer.getLayoutParams();
        if (titleBar.getVisibility() == View.VISIBLE) {
            contentParams.topMargin = titleBarHeight;
        } else {
            contentParams.topMargin = 0;
        }
        baseContentContainer.setLayoutParams(contentParams);

        if (StatusBarHelper.isSupportLightMode()) {
            compatPrimaryDark.setBackgroundColor(context.getResources().getColor(R.color.colorPrimaryDark));
        } else {
            compatPrimaryDark.setBackgroundColor(context.getResources().getColor(R.color.colorPrimaryDarkCompat));
        }
        titleBar.setBackgroundColor(context.getResources().getColor(R.color.colorPrimary));
    }

    public void setTitleBarStyle(@StyleRes int styleRes) {
        initAttrs(styleRes);
    }

    public void setTitleBarVisibility(int visibility) {
        realTitleBar.setVisibility(visibility);

        ViewGroup.LayoutParams params = titleBar.getLayoutParams();
        params.height = StatusBarHelper.compatStatusBarHeight;
        titleBar.setLayoutParams(params);

        ViewGroup.LayoutParams darkParams = compatPrimaryDark.getLayoutParams();
        darkParams.height = StatusBarHelper.compatStatusBarHeight;
        compatPrimaryDark.setLayoutParams(darkParams);

        compatPrimaryDark.setBackgroundColor(context.getResources().getColor(
                R.color.colorPrimaryDark));
        titleBar.setBackgroundColor(context.getResources().getColor(R.color.colorPrimary));
    }

    private void initStatusBarHeight() {
        if (titleBarHeight == 0) {
            titleBarHeight = StatusBarHelper.getCompatStatusBarHeight(context)
                    + context.getResources().getDimensionPixelSize(R.dimen.cupertino_title_bar_height);
        }
    }

}
